package com.epfly.promo.activity.dao;

import com.epfly.iface.promo.activity.enums.ActivityExecResultEnum;
import com.epfly.promo.activity.entity.ActivityTaskEntity;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.additional.insert.InsertListMapper;
import tk.mybatis.mapper.common.BaseMapper;

import java.util.List;
@Mapper
public interface ActivityTaskMapper extends BaseMapper<ActivityTaskEntity>, InsertListMapper<ActivityTaskEntity> {


    @ResultMap(value = "activityTaskMapperBaseResultMap")
    @Select(value = "select id, activity_id, taskType, next_try_time, " +
            " execute_times, latest_in_running_time, start_time, end_time, exec_result from tbl_activity_task " +
            " where del_flag = 0 and exec_result in (0, 3) and next_try_Time <= #{currentTime} and execute_times < #{maxExecuteTimes} limit #{maxLimit}")
    List<ActivityTaskEntity> getTodoList(@Param(value = "currentTime") long currentTime, @Param(value = "maxExecuteTimes") int maxExecuteTimes, @Param(value = "maxLimit") int maxLimit);


    int updateExecResultAndLatestInRunningTime(@Param(value = "status") Byte status, @Param(value = "currentTime") long currentTime, @Param(value = "taskIdList") List<Long> taskIdList);

    /**
     *  记录任务最近一次执行开始时间
     */
    @Update(value = "update tbl_activity_task set start_time = #{startTime} where id = #{id}")
    int recordLatestStartTime(@Param(value = "id") Long id, @Param(value = "startTime") long startTime);

    /**
     *  记录任务最近一次执行结束时间
     */
    @Update(value = "update tbl_activity_task set end_time = #{endTime} where id = #{id}")
    int recordLatestEndTime(@Param(value = "id") Long id, @Param(value = "endTime") long endTime);

    @Select(value = "select count(*) from tbl_activity_task where exec_result = 3 and execute_times >= #{maxExecuteTimes} and del_flag = 0 ")
    int getFailureTaskCount(@Param(value = "maxExecuteTimes") int maxExecuteTimes);

    /**
     * 如果执行次数大于0，执行状态置为"执行失败" {@link ActivityExecResultEnum#FAILURE}
     * 如果执行次数不大于0，执行状态置为"待执行" {@link ActivityExecResultEnum#WAIT}
     *
     * @param runningStatus 原执行状态为"执行中"
     * @param waitStatus 目标状态
     * @param failureStatus 目标状态
     * @param timeAgo 入线程池时间为半个小时前
     */
    @Update(value = "update tbl_activity_task set exec_result = IF(execute_times > 0, #{failureStatus}, #{waitStatus}) " +
            " where latest_in_running_time > #{timeAgo} and exec_result = #{runningStatus} and del_flag = 0 ")
    int resetPooledTask(@Param(value = "runningStatus") Byte runningStatus,
                            @Param(value = "waitStatus") Byte waitStatus, @Param(value = "failureStatus") Byte failureStatus,
                            @Param(value = "timeAgo") long timeAgo);

    @Select(value = "select id from tbl_activity_task where  activity_id = #{activityId} and task_type = #{taskType} and exec_result in (0, 3) ")
    ActivityTaskEntity getPreTask(@Param(value = "activityId") Long activityId, @Param(value = "taskType") Byte taskType);
}
